package net.siufung.market.base.user.controller.regulation;

import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.VerifyRepeatDto;
import net.siufung.core.model.enums.MethodEventEnum;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.market.base.user.controller.model.dto.UserSaveDto;
import net.siufung.market.base.user.mapper.IUserMapper;

/**
 * @author 陈建峰
 * @since 2022/4/18 2:09 下午
 */
@AllArgsConstructor
public class UserCreateRegulation extends AbstractRegulation<UserSaveDto> {

    private static final long serialVersionUID = 4278919973675557941L;

    private final IUserMapper userMapper;

    @Override
    public boolean isSatisfiedBy(UserSaveDto userSaveDto) {
        VerifyRepeatDto<Integer> verifyRepeatDto = new VerifyRepeatDto<>();
        verifyRepeatDto.setMethodEventEnum(MethodEventEnum.ADD);
        verifyRepeatDto.setPk(null);
        verifyRepeatDto.setVerifyKeyValue(userSaveDto.getLoginName());
        if(!userMapper.checkLoginNameNotRepeat(verifyRepeatDto)){
            throw new BizServiceException("登录名重复！");
        }
        verifyRepeatDto.setVerifyKeyValue(userSaveDto.getMobile());
        if(!userMapper.checkMobileNotRepeat(verifyRepeatDto)){
            throw new BizServiceException("手机号重复！");
        }
        verifyRepeatDto.setVerifyKeyValue(userSaveDto.getUserCode());
        if(!userMapper.checkUserCodeNotRepeat(verifyRepeatDto)){
            throw new BizServiceException("用户编码重复！");
        }
        return Boolean.TRUE;
    }



}
